<?php
/**
 * @version $Id: view.html.php 48 2011-09-28 06:47:49Z lnmtrung $
 */
 
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view' );

class WrdViewProducts extends WrdView{
	function display($tpl = null) {
		$this->setModel($this->getModel('products'), true);
		$fnc = 'display'.ucfirst($this->getLayout());
		if (is_callable(array(&$this, $fnc)))
			$this->$fnc($tpl);
		else
			parent::display($tpl);
	}

	function displayDefault($tpl = null){
		
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		
		
		$document = & JFactory::getDocument();
		$document->setTitle( JText::_('Products Manager') );

		JToolBarHelper::title(JText::_('Products Manager'),'country.png');
 		JToolBarHelper::addNew();
		JToolBarHelper::deleteList();
		WrdHelper::addSubMenu('config',$view);
		$this->sidebar = JHtmlSidebar::render();

		$filter_order     		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_item','filter_order','id' );
		$filter_order_Dir 		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_Dir','filter_order_Dir', '' );

		$lists['order'] 		= $filter_order;
		$lists['order_Dir'] 	= $filter_order_Dir;
		$items					= & $this->get( 'Data');
		$total					= & $this->get( 'Total');
		$pagination 			= & $this->get( 'Pagination' );
		 
		$search 				= $mainframe->getUserStateFromRequest( $context.$view.'search','search','','string' );
		$filter_pcate 			= $mainframe->getUserStateFromRequest( $context.$view.'cat_id','cat_id',-1,'int' );
		$filter_state 			= $mainframe->getUserStateFromRequest( $context.$view.'published','published',-1,'int' );
		$filter_user			= $mainframe->getUserStateFromRequest( $context.$view.'created_id','created_id',-1,'int' );
		
		$search 				= JString::strtolower( $search );
		
		$lists['search'] 		= $search;
		$lists['published'] 	= JHTML::_('wrd.published',$filter_state,'onchange="this.form.submit()"');
		$lists['user'] 			= JHTML::_('wrd.listUser','created_id',$filter_user,'onchange="this.form.submit()"','products');
		
	    $this->assignRef('search',		$search);
	    $this->assignRef('lists',		$lists);
	  	$this->assignRef('items',		$items);
	    $this->assignRef('pagination',	$pagination);
	    parent::display($tpl);
  	}
  
  	function displayForm($tpl = null){
  		
  		$lang = new LanguagesModelInstalled();
  		$current_languages = $lang ->getData();
  		
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		$user = JFactory::getUser();
		$cid = JRequest::getInt('id');
		$model	=& $this->getModel();
		
		$detail = array();		
		if($cid != 0){
			$detail	=& $model->getItem($cid);
			$filesalbum = WrdHelper::getFiles($cid,$user->id);
			//print_r($filesalbum);
			$filesalbum1 = WrdHelper::getFiles($cid,$user->id,'image1');
			$filesalbum2 = WrdHelper::getFiles($cid,$user->id,'image2');
		}
		else{
			$filesalbum = WrdHelper::getFiles($cid,$user->id);
			$filesalbum1 = WrdHelper::getFiles($cid,$user->id,'image1');
			$filesalbum2 = WrdHelper::getFiles($cid,$user->id,'image2');
		}
		
		$categories = array();
		$tree=$this->hobbiesTree($detail);
		
		$categories = array_merge($categories,$tree);
		$parent = JHTML::_('select.genericlist', $categories, 'cat_id', 'class="cat_id"', 'value', 'text', $detail->cat_id);
		
		
		$lists = array();
		$lists['published'] 		= JHTML::_('wrd.published',$detail->published,'',0 );

		$wysiwyg = &JFactory::getEditor();

		$text = ($detail->id < 1) ? JText::_( 'NEW' ) : JText::_( 'EDIT' );
		JToolBarHelper::title(   JText::_( 'Products' ).': <small><small>['.$text.']</small></small>','country.png' );
		JToolBarHelper::save();
		JToolBarHelper::cancel( 'cancel', 'Close' );
		
		$user->current_product = $detail;
		$this->assignRef('current_languages',		$current_languages);
		$this->assignRef('data',		$detail);
		$this->assignRef('lists',		$lists);
		$this->assignRef('wysiwyg',		$wysiwyg);
		$this->assignRef('user',		$user);
		$this->assignRef('parent', 		$parent);
		$this->assignRef('filesalbum',	$filesalbum);
		$this->assignRef('filesalbum1',	$filesalbum1);
		$this->assignRef('filesalbum2',	$filesalbum2);
		
		parent::display($tpl);
	}
	
	function hobbiesTree($row = NULL) {
		$db = JFactory::getDBO();

		$query = "SELECT m.* FROM #__wrd_pcategory m ORDER BY m.parent_id";
		
		$db->setQuery($query);
		$mitems = $db->loadObjectList();

		$children = array ();
		if ($mitems) {
			foreach ($mitems as $v) {
				$pt = $v->parent_id;
				$list = @$children[$pt]?$children[$pt]: array ();
				array_push($list, $v);
				$children[$pt] = $list;
			}
		}
		
		$list = JHTML::_('menu.treerecurse,', 0, '', array (), $children, 9999, 0, 1);
		$mitems = array();
		$mitems[] = JHTML::_ ( 'select.option', '0', JText::_ ( '-- None --' ));
		foreach ($list as $item) {
			$itreename = JString::str_ireplace('&#160;', '', $item->treename);
			$origin = array('&lt;sup&gt;', '/&lt;sup&gt;', '<sup>', '</sup>','.');
			$des_str   = array('', '', '', '','....');
			$item->treename = $this->convertString($origin,$des_str,$itreename);
			$mitems[] = JHTML::_('select.option', $item->id,$item->treename);
		}
		return $mitems;
	}
	
	function convertString($origin='',$des_str='',$string = ''){
		$output  = str_replace($origin, $des_str, $string);
		return $output;	
	}
}
?>